package edu.chl.angholt.bookproject.db.entity;

import java.io.Serializable;
import java.util.Calendar;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;

/**
 * Entity class to store informashen about books.
 * @author Patric Peterson
 */
@Entity
public class Book implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long bookId;
        
    @Column(name="first_release")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Calendar firstRelease;
    
    @Column(length=1023)
    private String synopsis;
    
    @Column(length=63, nullable=false,unique=true)
    private String title;
    
    @Column(name="original_language",length=63)
    private String originalLanguage;
    
    @Column(nullable=false,length=126)
    private String publisher;
    
    @ManyToOne(cascade={CascadeType.REFRESH, CascadeType.MERGE})
    @JoinColumn(nullable=false)
    private Author author;
    
    /**
     * Construct a Book class with all basic data.
     * @param firstRelease Date of first release.
     * @param synopsis Synopsis of book. ( less then 1023 chars )
     * @param title Title of book. ( less then 63 chars ) ( needed be for persisting )
     * @param OriginalLanguage Name of the language the book was originaly writen in. ( less then 63 chars )
     * @param publisher The publisher of this book. ( less  then 126 chars )
     * @param author the author of this book. ( needed be for persisting )
     */
    public Book(Calendar firstRelease, String synopsis, String title, String OriginalLanguage, String publisher, Author author) {
        this.firstRelease = firstRelease;
        this.synopsis = synopsis;
        this.title = title;
        this.originalLanguage = OriginalLanguage;
        this.publisher = publisher;
        this.author = author;

    }

    
    /**
     * Construct a Book class with no data.
     * Need to add author, publisher, title..
     */
    public Book() {
    }
    
    /**
     * Get entity id.
     * @return entity id.
     */
    public Long getBookId() {
        return bookId;
    }

    /**
     * set entity id.
     * shout not be changed exept by jpa.
     * @param id entity id
     */
    public void setBookId(Long id) {
        this.bookId = id;
    }

    /**
     * Get the name of the language the book was origenaly writen in.
     * @return Name of the language.
     */
    public String getOriginalLanguage() {
        return originalLanguage;
    }

    /**
     * Set the name of the language the book was origenaly writen in. ( less then 63 chars )
     * @param originalLanguage Name of the language.
     */
    public void setOriginalLanguage(String originalLanguage) {
        this.originalLanguage = originalLanguage;
    }

    /**
     * Get author of book.
     * @return author of the book.
     */
    public Author getAuthor() {
        return author;
    }

    /**
     * Set author of book.
     * @param author author of the book.
     */
    public void setAuthor(Author author) {
        this.author = author;
    }

    /**
     * Get the date of the first release of this book.
     * @return date of the first release of this book.
     */
    public Calendar getFirstRelease() {
        return firstRelease;
    }

    /**
     * Get the date of the first release of this book.
     * @param firstRelease Date of the first release of this book.
     */
    public void setFirstRelease(Calendar firstRelease) {
        this.firstRelease = firstRelease;
    }

    /**
     * Get the title of the book.
     * @return Title of the book.
     */
    public String getTitle() {
        return title;
    }

    /**
     * Set the title of the book. ( less then 63 chars )
     * @param title Title of the book.
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Get the publisher of the book.
     * @return Publisher of the book.
     */
    public String getPublisher() {
        return publisher;
    }

    /**
     * Set the publisher of the book.
     * @param publisher Publisher of the book.
     */
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }


    /**
     * Get the synopsis for the book of the book.
     * @return Synopsis for the book of the book.
     */
    public String getSynopsis() {
        return synopsis;
    }

    /**
     * Set the synopsis for the book of the book. ( less then 1023 chars )
     * @param synopsis Synopsis for the book of the book.
     */
    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (bookId != null ? bookId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TO DO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Book)) {
            return false;
        }
        Book other = (Book) object;
        if ((this.bookId == null && other.bookId != null) || (this.bookId != null && !this.bookId.equals(other.bookId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.angholt.bookproject.db.entity.Book[ id=" + bookId + " ]";
    }
    
}
